<?php
namespace App\Models\Rating;

use App\Models\Model;


class Check extends Model
{
    protected $appends = [
        'submit_text','materials_array','files_array','leader_array','members_array','teachers_array'
    ];

    public function check_teachers()
    {
        return $this->hasMany(CheckTeacher::class);
    }

    public function check_teacher()
    {
        return $this->hasOne(CheckTeacher::class);
    }


    //申报状态
    public function getSubmitTextAttribute(){
        $check_start = !empty($this->attributes['check_start'])?strtotime($this->attributes['check_start']):'';
        $check_end = !empty($this->attributes['check_end'])?strtotime($this->attributes['check_end'])+86399:''; //因为没有具体时分秒，所以加一天(少一秒)时间
        $status = !empty($this->attributes['status']) ? $this->attributes['status'] : '';
        if($check_start && $check_end){
            $now_time = time();
            if($status == 0){
                return '待提交';
            }
            if($now_time < $check_start){
                return '未开始';
            }else if($now_time >= $check_start && $now_time<=$check_end){
                return '进行中';
            }else if($now_time > $check_end){
                return '已结束';
            }else{
                return '';
            }
        }
       
        return '待提交';
    }

    

    public function getMaterialsArrayAttribute(){
        $materials_date = !empty($this->attributes['materials_date'])?$this->attributes['materials_date']:'';
        if(!$materials_date){
            return [];
        }
        return json_decode($materials_date,true);
    }

    public function getFilesArrayAttribute(){
        $files = !empty($this->attributes['files'])?$this->attributes['files']:'';
        if(!$files){
            return [];
        }
        return json_decode($files,true);
    }

    public function getLeaderArrayAttribute(){
        $leader = !empty($this->attributes['leader'])?$this->attributes['leader']:'';
        if(!$leader){
            return [];
        }
        $leader = json_decode($leader,true);
        return [
            'name' => array_column($leader,'name'),
            'value' => array_column($leader,'value'),
            'all' => $leader
        ];
    }

    public function getMembersArrayAttribute(){
        $members = !empty($this->attributes['members'])?$this->attributes['members']:'';
        if(!$members){
            return [];
        }
        $members = json_decode($members,true);
        return [
            'name' => array_column($members,'name'),
            'value' => array_column($members,'value'),
            'all' => $members
        ];
    }

    public function getTeachersArrayAttribute(){
        $teachers = !empty($this->attributes['teachers'])?$this->attributes['teachers']:'';
        if(!$teachers){
            return [];
        }
        $teachers = json_decode($teachers,true);
        return [
            'name' => array_column($teachers,'name'),
            'value' => array_column($teachers,'value'),
            'all' => $teachers
        ];
    }


}